import Duck from 'extensible-duck'
import produce from 'immer'

export default new Duck({
  namespace: 'admin',
  store: 'global',
  types: [
    'LOAD_USER',
    'RECIVE_USER',
    'REMOVE_USER',
    'SIDE_NAV_TOOGLE',
    'SIDE_NAV_HIDE',
    'INIT',
    'INIT_FINISH',
    'SIGNIN',
    'ACTIVE_NAV',
    'DELAY_ACTION',
    'RETURN_URL',
    'AUTHED',
    'CHECK_RETURN_URL',
    'RESET_RETURN_URL',
    'NEW_DATASET_ITEM',
  ],
  initialState: { sideNav: true, isInit: false },
  reducer: produce((draft, action, duck) => {
    switch (action.type) {
      case duck.types.REMOVE:
        draft.user = undefined
        break
      case duck.types.RECIVE_USER:
        draft.user = action.user
        break
      case duck.types.SIDE_NAV_TOOGLE:
        draft.sideNav = !draft.sideNav
        break
      case duck.types.SIDE_NAV_HIDE:
        draft.sideNav = false
        break
      case duck.types.SIGNIN:
        draft.user = action.data.user
        break
      case duck.types.INIT_FINISH:
        draft.isInit = true
        break
      case duck.types.ACTIVE_NAV:
        draft.activeNav = action.value
        break
      case duck.types.RETURN_URL:
        draft.reutrnUrl = action.url
        break
      case duck.types.RESET_RETURN_URL:
        draft.reutrnUrl = undefined
        break
      case duck.types.AUTHED:
        draft.auth = action.auth
        break
      default:
        break
    }
  }),
  selectors: {
    user: state => state.global.user,
    userId: state => (state.global.auth ? state.global.auth.sub : undefined),
    authInfo: state => state.global.auth,
    isSignedIn: state => state.global.user !== undefined,
    isAuthed: state => state.global.auth !== undefined,
    returnUrl: state => state.global.reutrnUrl,
    sideNavVisibale: state => state.global.sideNav,
    isGlobalInit: state => state.global.isInit,
    activeNavItem: state => state.global.activeNav,
  },
  creators: duck => ({
    actionLoadUser: () => ({ type: duck.types.LOAD_USER }),
    actionReciveUser: user => ({ type: duck.types.RECIVE_USER, user }),
    actionRemoveUser: () => ({ type: duck.types.REMOVE_USER }),
    actionToogleSideNav: () => ({ type: duck.types.SIDE_NAV_TOOGLE }),
    actionShowSideNav: () => ({ type: duck.types.SIDE_NAV_HIDE }),
    actionInitGlobal: () => ({ type: duck.types.INIT }),
    actionInitFinished: () => ({ type: duck.types.INIT_FINISH }),
    actionSignIn: data => ({ type: duck.types.SIGNIN, data }),
    actionSetActiveNavItem: value => ({ type: duck.types.ACTIVE_NAV, value }),
    actionDelayAction: (action, time, ...params) => ({
      type: duck.types.DELAY_ACTION,
      action,
      time,
      params,
    }),
    actionSetReturnUrl: url => ({ type: duck.types.RETURN_URL, url }),
    actionAuthed: auth => ({ type: duck.types.AUTHED, auth }),
    actionResetReturnUrl: () => ({ type: duck.types.RESET_RETURN_URL }),
    actionCheckReturnUrl: () => ({ type: duck.types.CHECK_RETURN_URL }),
    actionNewDatasetItem: config => ({ type: duck.types.NEW_DATASET_ITEM, config }),
  }),
})
